$(function(){
	var ary = new Array();
	var $modal = $('#my-modal-loading');
	var jsonStr;
	getSearchData();
	
	function getSearchData(){
		$modal.modal();
		$('ul li').remove();
		$.ajax({
			type: "POST",
			url: "http://yx.fxtourong.com/V1/Circle/searchCircle",
			data: {
				token : localStorage.getItem("token"),
				keyword: '圈'
			},
			dataType: "json",
			success: function(data) {
				$("#empty_img").css("display", "none");
				$modal.modal('close');
				var code = data.code;
				console.log("code:" + data.code);
				if(code == "1") {
					jsonStr = data.data;
					for(var i = 0; i<jsonStr.length; i++){
						var industry = jsonStr[i];
						ary.push(industry);
						var is_pay = industry.is_pay;
						console.log("qqqqqq:" + is_pay);
						var price = "免费";
						if(is_pay == "0"){
							price = "免费";
						} else {
							price = industry.price;
						}
						var is_pwd = industry.is_encrypt;
						var pwd = "免密";
						if(is_pwd == "0"){
							pwd = "免密";
						} else {
							pwd = "加密";
						}
						var juse = industry.nickname;
						var juseType = "圈主:" + juse;
						
					var timeStr = industry.created_time;
						$("#search_ul").append("<li class='am-g' value='" + i + "' onclick=''>" + 
					        "<div class='am-u-sm-3'>" + 
					        	"<img src='" + industry.cover + "' style='width: 50px;height: 50px;margin-left: 20px;display: inline;margin-top: 10px;margin-bottom: 10px;'/>" + 
					        "</div>" + 
					        "<div class='am-u-sm-9 am-list-main' style='margin-top:8px;'>" +  
					            "<div class='am-list-item-hd'><span style='font-size: 15px;margin-left:-10px;'>" + industry.name + "</span>" + 
					            "<button value='" + i + "' type='button' class='am-btn am-btn-secondary am-round add_circle' style='float: right;margin-right: 10px;display:inline;font-size:12px;' onclick=''>加入圈子</button>"+ "</div>" +
					            "<div class='am-list-item-text' style='margin-top: 2px;margin-left:-10px;'>" + 
					            "<span style='font-size: 14px;color: #BBBBBB;'>" + juseType + "</span>" + 
					            "<span style='font-size: 14px;color: #B6940A;margin-left:10px;'>" + price + "</span></div>" +
								"<div class='am-list-item-text' style='margin-top: 2px;margin-left:-10px;'>" +
								"<span style='font-size: 14px;color: #BBBBBB;float: right;margin-right: 10px;'>" + timeStr + "</span>" + "</div>" +
					        "</div>" + "</li>");
//					  	$("#" + "addQuery" + i).click(function(){
//					  		alert(industry.name);
//					  	});
					}
				} else {
					$modal.modal('close');
					$("#empty_img").css("display", "block");
				}
			}
		});
	}
	
	$(document).on("click", ".add_circle", function() { //加入圈子按钮
		$modal.modal();
		var m = $(this).val();
		var ispay = ary[m].is_pay;
		var approvalStr = ary[m].approval;
		if(ispay == "0"){//免费的
				if(approvalStr == "0"){//不需要审批
					var idStr = ary[m].id;
					var url = "http://yx.fxtourong.com/V1/Member/memberJoinCircle" + "?store_id=" + idStr;
					$.ajax({
						type: "POST",
						url: url,
						data: {
							token : localStorage.getItem("token")
						},
						dataType: "json",
						success: function(data) {
							$modal.modal('close');
							console.log(data);
							var code = data.code;
							console.log(code);
							if(code == "1") {
								new Toast({context:$('body'),message:'加入成功'}).show();
								localStorage.setItem("query_id", ary[m].id);
								location.href = "query_detail.html";
							} else if(code == "2") {
								alert("您已经加入该圈子");
							} else {
								alert("加入失败:" + data.message);
							}
						},
						error: function(e){
							$modal.modal('close');
							alert('服务请求异常！');
						}
					});
				} else {	//	需要审批
					var idStr = ary[m].id;
					var url = "http://yx.fxtourong.com/V1/Circle/memberJoinNeedApprovalCircle" + "?store_id=" + idStr;
					$.ajax({
						type: "POST",
						url: url,
						data: {
							token : localStorage.getItem("token")
						},
						dataType: "json",
						success: function(data) {
							$modal.modal('close');
							console.log(data);
							var code = data.code;
							console.log(code);
							if(code == "1") {
								alert("已发送申请给圈主");
							} else {
								alert("申请失败:" + data.message);
							}
						},
						error: function(e){
							$modal.modal('close');
							alert("服务请求异常！");
						}
					});
				}
			} else {//付费的
//				$modal.modal();
					wx.chooseWXPay({
										timestamp: timestamp, // 支付签名时间戳，注意微信jssdk中的所有使用timestamp字段均为小写。但最新版的支付后台生成签名使用的timeStamp字段名需大写其中的S字符
									    nonceStr: nonceStr, // 支付签名随机串，不长于 32 位
									    package: '', // 统一支付接口返回的prepay_id参数值，提交格式如：prepay_id=***）
									    signType: signature, // 签名方式，默认为'SHA1'，使用新版支付需传入'MD5'
									    paySign: '', // 支付签名
									    success: function (res) {
									        	$modal.modal('close');
												location.href = "query_detail.html";
											},
										error: function(e){
												$modal.modal('close');
												alert("支付失败");
										}
									});
			}
		return false;
	});
});
